package edu.spec.sms.controller;

import edu.spec.sms.model.Student;
import edu.spec.sms.model.event.Event;
import edu.spec.sms.service.iface.IEventService;
import edu.spec.sms.service.iface.IStudentEventService;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.enumeration.AttendStatus;
import edu.spec.sys.util.XlsTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * Created by MadFrog on 2017/6/12.
 */
@Controller
@RequestMapping("/admin/studentEvent")
public class StudentEventController {

    @Inject
    private IEventService eventService;
    @Inject
    private IStudentEventService studentEventService;

    @ResponseBody
    @RequestMapping("/listStudentEvent")
    public MvcReturn listStudentEventByStudentId(HttpSession session){
        Student currentStu = (Student)session.getAttribute("user");
        Map<String, List<Event>> emap = eventService.listStudentEvent(currentStu.getId());
        return new MvcReturn(true, emap);
    }

    @ResponseBody
    @RequestMapping("/enroll/{eventId}")
    public MvcReturn enrollEvent( @PathVariable Integer eventId, HttpSession session){
        Student currentStu = (Student)session.getAttribute("user");
        eventService.attend(currentStu.getId(), eventId);
        return new MvcReturn(true);
    }

    @RequestMapping("/signIn/{eventId}")
    public String eventSignIn(@PathVariable Integer eventId, HttpSession session){
        String openId = (String)session.getAttribute("openId");
        Map m = studentEventService.signIn(openId, eventId);
        session.setAttribute("message", m.get("msg"));
        if((Boolean) m.get("success")==true){
            return "common/optSuc";
        }else {
            return "common/optFail";
        }
    }

    @ResponseBody
    @RequestMapping("/listStudentByCon")
    public MvcReturn listStudentByCon(Integer eventId, Integer clazzId, AttendStatus attendStatus){
        if(eventId==null || clazzId==null){
            return new MvcReturn(false, "请选择活动或班级!");
        }
        List<Student> stuList = studentEventService.listStudentByCon(eventId, clazzId, attendStatus);
        return new MvcReturn(true, stuList);
    }

    @RequestMapping("/exportOut/{eventId}")
    public void exportOut(HttpServletRequest req, HttpServletResponse resp, @PathVariable Integer eventId){
        List<Student> studentList = studentEventService.listAllStudentByEvent(eventId);
        Event event = eventService.load(eventId);
        ServletOutputStream fout = null;
//        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-HH-mm");
        String filename = URLEncoder.encode(event.getName() + "情况导出");
        try {
            if (req.getHeader("user-agent").indexOf("MSIE") != -1) {
                filename = URLEncoder.encode(filename, "utf-8") + ".xls";
            } else {
                filename = new String(filename.getBytes("utf-8"), "iso-8859-1") + ".xls";
            }
            resp.setHeader("Content-Disposition", "attachment;filename=" + filename);
            resp.setContentType("application/msexcel");
            fout = resp.getOutputStream();
            XlsTools.exportExcel("导出", Student.class, studentList, fout);
            fout.flush();
            fout.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}